<template>
  <view class="container">
    <uni-forms ref="form" :modelValue="form" labelWidth="120px" labelPosition="top">
      <view class="hidden-title">分析</view>
      <view class="flex justify-between items-center pr-30">
        <view class="flex items-center blue" style="cursor: pointer" @click="plusRow">
          <image class="icon-style" src="/static/images/icon-add.png" mode="aspectFill"></image>
          继续添加分析数据
        </view>
      </view>
      <view class="background-white bor-top">
        <template v-for="(item, index) in form.analysisList">
          <uni-forms-item
            label="分析点"
            required
            :rules="[{ required: true, errorMessage: '分析点必填' }]"
            :key="item.id"
            :name="['analysisList', index, 'analysisPoint']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.analysisList[index].analysisPoint" placeholder="请输入分析点" />
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="分析对象"
            required
            :rules="[{ required: true, errorMessage: '分析对象必填' }]"
            :key="item.id"
            :name="['analysisList', index, 'analysisTarget']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.analysisList[index].analysisTarget" placeholder="请输入分析对象" />
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="阈值"
            required
            :rules="[{ required: true, errorMessage: '阈值必填' }]"
            :key="item.id"
            :name="['analysisList', index, 'analysisThreshold']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.analysisList[index].analysisThreshold" placeholder="请输入阈值" />
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="分析数据"
            required
            :rules="[{ required: true, errorMessage: '分析数据必填' }]"
            :key="item.id"
            :name="['analysisList', index, 'analysisData']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.analysisList[index].analysisData" placeholder="请输入分析数据" />
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="分析时间"
            required
            :rules="[{ required: true, errorMessage: '分析时间必填' }]"
            :key="item.id"
            :name="['analysisList', index, 'analysisTime']"
          >
            <view class="form-item">
              <uni-datetime-picker
                type="datetime"
                :hideSecond="true"
                v-model="form.analysisList[index].analysisTime"
                placeholder="请输入分析时间"
              />
              <!-- <uni-easyinput v-model="form.analysisList[index].analysisTime" @focus="timeClick(index)" placeholder="请输入分析数据" /> -->
            </view>
          </uni-forms-item>
        </template>

        <!-- <uni-forms-item name="analysisPoint" label="分析点">
					<textarea v-model="item.analysisPoint" class="text-area" placeholder="简要说明审批意见"
						maxlength="50" />
				</uni-forms-item>
				<uni-forms-item name="analysisTarget" label="分析对象">
					<textarea v-model="item.analysisTarget" class="text-area" placeholder="简要说明审批意见"
						maxlength="50" />
				</uni-forms-item>
				<uni-forms-item name="analysisThreshold" label="阈值">
					<textarea v-model="item.analysisThreshold" class="text-area" placeholder="简要说明审批意见"
						maxlength="50" />
				</uni-forms-item>
				<uni-forms-item name="analysisData" label="分析数据">
					<textarea v-model="item.analysisData" class="text-area" placeholder="简要说明审批意见"
						maxlength="50" />
				</uni-forms-item> -->
      </view>
      <view class="background-white">
        <uni-forms-item name="opinionType" class="acceptance" required>
          <view slot="label">
            <view class="flex align-center">
              <view class="mr-20">验收意见</view>
              <view class="acceptance-list">
                <view
                  class="acceptance-item"
                  v-for="(item, index) in acceptanceList"
                  :key="item.code"
                  :class="{ active: form.opinionType == item.dictValue }"
                  @click="form.opinionType = item.dictValue"
                >
                  <image
                    class="icon-style"
                    v-show="form.opinionType != item.dictValue"
                    src="/static/images/work/dbd//selected_no.png"
                    mode="aspectFill"
                  ></image>
                  <image
                    class="icon-style"
                    v-show="form.opinionType == item.dictValue"
                    src="/static/images/work/dbd//selected_yes.png"
                    mode="aspectFill"
                  ></image>
                  <text class="content">{{ item.dictLabel }}</text>
                </view>
              </view>
            </view>
          </view>
        </uni-forms-item>
      </view>
      <view class="background-white bor-top" v-if="form.opinionType === 'REPULSE'">
        <uni-forms-item name="targetNodeId" label="选择回退节点" required>
          <cu-cell
            title="回退节点"
            required
            link
            placeholder="请选择"
            :description="taskNode"
            @click="rollbackClick()"
          ></cu-cell>
        </uni-forms-item>
      </view>
      <view class="background-white bor-top">
        <uni-forms-item name="opinion" label="审查意见说明" required>
          <uni-easyinput
            type="textarea"
            v-model="form.opinion"
            placeholder="简要说明审批意见"
            maxlength="50"
          ></uni-easyinput>
        </uni-forms-item>
      </view>
    </uni-forms>
    <view class="submit-group">
      <button class="u-button" :ripple="true" @click="submit">确定</button>
    </view>
    <CuDateTimePicker ref="timePicker" @change="timePickerChange" />
    <CuSelect
      v-if="form.opinionType === 'REPULSE'"
      ref="rollbackDict"
      title="选择回退节点"
      :options="backNodes"
      @update:value="rollbackConfirm"
    ></CuSelect>
  </view>
</template>

<script>
import CuSelect from '@/components/CuSelect/index.vue'
import CuDateTimePicker from '@/components/cu-date-time-picker/cu-date-time-picker.vue'
import { analysis, getNode } from '@/api/repair/repair.js'
import { getInfo } from '../../../../../../api/login'

export default {
  components: {
    CuSelect,
    CuDateTimePicker
  },
  data() {
    return {
      acceptanceList: [
        { dictLabel: '通过', dictValue: 'PASS' },
        { dictLabel: '指定回退', dictValue: 'REPULSE' }
      ],
      backNodes: [],
      form: {
        opinionType: '',
        opinion: '',
        targetNodeId: '',
        analysisList: [
          {
            analysisPoint: '',
            analysisTarget: '',
            analysisThreshold: '',
            analysisData: '',
            analysisTime: ''
          }
        ]
      },
      taskNode: '',
      targetNodeTypeValue: '',
      id: '',
      procTaskId: '',
      index: '',
      rules: {
        opinionType: {
          rules: [
            {
              required: true,
              errorMessage: '验收意见不能为空'
            }
          ]
        },
        targetNodeId: {
          rules: [
            {
              required: true,
              errorMessage: '回退节点不能为空'
            }
          ]
        },
        opinion: {
          rules: [
            {
              required: true,
              errorMessage: '审查意见说明不能为空'
            }
          ]
        }
      }
    }
  },
  onReady() {
    this.$refs.form.setRules(this.rules)
  },
  onLoad(option) {
    if (!option.data) return
    let data = JSON.parse(option.data)
    this.id = data.id
    this.procTaskId = data.procTaskId
    this.getBackNode(this.procTaskId)
  },
  methods: {
    getBackNode(procTaskId) {
      getNode(procTaskId).then((res) => {
        this.backNodes = res.data
        this.backNodes.map((m) => {
          m.label = m.nodeName
          m.value = m.nodeId
        })
      })
    },
    async plusRow() {
      const { user } = await getInfo()
      let row = { analysisPoint: '', analysisTarget: '', analysisThreshold: '', analysisData: '', analysisTime: '' }
      this.form.analysisList.push(row)
    },
    timeClick(index) {
      this.index = index
      this.$refs.timePicker.show()
    },
    timePickerChange(value) {
      let time = value.replaceAll('/', '-')
      this.form.analysisList[this.index].analysisTime = time
    },
    rollbackClick() {
      this.$refs.rollbackDict.open()
    },
    rollbackConfirm(obj) {
      this.taskNode = obj.name
      this.form.targetNodeId = obj.value
      let match = this.backNodes.find((n) => {
        return n.value === obj.value
      })
      this.targetNodeTypeValue = match.nodeTypeValue
    },
    async submit() {
      const { user } = await getInfo()
      this.form.analysisList.map((m) => {
        m.analysisUserName = user.name
      })
      this.$refs.form.validate().then((res) => {
        let data = {
          id: this.id,
          procTaskId: this.procTaskId,
          targetNodeId: this.form.targetNodeId,
          targetNodeTypeValue: this.targetNodeTypeValue,
          opinion: this.form.opinion,
          deviceExamineWorkflowEntity: {
            examineId: this.id,
            opinionType: this.form.opinionType
          },
          analysisList: this.form.analysisList
        }
        analysis(data).then((res) => {
          this.$modal.showToast('处理成功')
          uni.$emit('refresh')
          uni.navigateBack()
        })
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  margin-bottom: 200rpx;

  .background-white {
    background-color: #fff;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .uni-forms-item__label {
      color: #333;
      font-size: 30rpx;
    }

    .bor-btm {
      display: flex;
      border-bottom: 1px solid #d5d7e3;
    }

    .photo {
      display: flex;
      justify-content: space-between;

      image {
        width: 214rpx;
        height: 214rpx;
      }
    }

    .text {
      padding-bottom: 20rpx;
      padding-top: 4rpx;
      color: #333;

      text {
        color: #666;
      }
    }
  }
}

.hidden-title {
  background: #f5f5f5;
  padding: 20rpx;
  color: #999;
}

.bor-top {
  border-top: 20rpx solid #f5f5f5;
}

.blue {
  color: #88b4c4;
}

.icon-style {
  width: 52rpx;
  height: 42rpx;
  padding-right: 10rpx;
}
.input {
  display: inline-block;
  width: 100%;
  background-color: #f5f5f5;
  padding: 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  height: 60rpx;
}
.acceptance {
  margin-bottom: 10rpx;
}
.acceptance-list {
  display: flex;

  .acceptance-item {
    display: flex;
    align-items: center;
    margin-left: 20rpx;
    color: #333;

    .icon-style {
      width: 50rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .content {
      flex: 1;
    }
  }
}
</style>
